--每日签到
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local LoginSignExcel = require("excel.loginSign")
local ItemExcel = require("excel.item")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
local GuideLogic = require("guide.GuideLogic")

SIGN_DAY = 31

STATUS_TYPE1 = 1   -- 已签到可领取
STATUS_TYPE2 = 2   -- 可以充值签到
STATUS_TYPE3 = 3   -- 充值后可领取
STATUS_TYPE4 = 4   -- 不能领取

function updateGM(human, day)
    local now = os.time()
    human.db.signIn = {startTime = now,upTime = now,status = {},cnt = 1}
    human.db.signIn.startTime = human.db.signIn.startTime - 24 * 60 * 60 * day 
    human.db.signIn.cnt =  day
    
end

local function checkDB(human)
    local now = os.time()
    human.db.signIn = human.db.signIn or {startTime = now,upTime = now,status = {},cnt = 1}
    local signInDB = human.db.signIn
    if not Util.isSameDay(signInDB.upTime) then
        signInDB.cnt = signInDB.cnt + 1
        signInDB.upTime = now
        print("  signInDB.cnt   ", signInDB.cnt )
        if signInDB.cnt > SIGN_DAY then
            human.db.signIn = {startTime = now,upTime = now,status = {},cnt = 1}
        else
            print("  not  signInDB.cnt   ", signInDB.cnt )
        end
    end
end

local function checkBuy(human)
    if (human.db.topupAcountDaily or 0) > 0 then
        return 1
    else
        return 0
    end
end

local function getState(day,signInDB,buyToday)
    if signInDB.cnt < day then
        return 0
    end

    if not signInDB.status[day] then
        return STATUS_TYPE1
    end

    if signInDB.status[day] == 1 and buyToday ~= 1 and signInDB.cnt == day then
        return STATUS_TYPE2
    end

    if signInDB.status[day] == 1 and buyToday == 1 and signInDB.cnt == day then
       return STATUS_TYPE3
    end

    return STATUS_TYPE4 
end

local function getConfig(human)
    return LoginSignExcel.firstMonth
end

function query(human)
    -- 初始化数据库数据
    checkDB(human)
    local signInDB = human.db.signIn
    local msgRet = Msg.gc.GC_LOGIN_SIGN_QUERY
    local now = os.time()
    local time = Util.getDayStartTime()
    msgRet.nextTime = time + 24 * 60 * 60 - now
    msgRet.curCnt = signInDB.cnt
    msgRet.maxCnt = SIGN_DAY
    msgRet.actID = 1
    local len = 0
    local buyToday = checkBuy(human)

    local config = getConfig(human)
    for k,v in ipairs(config) do
        len = len + 1
        local net = msgRet.list[len]
        net.state = getState(k,signInDB,buyToday)
        Grid.makeItem(net.item,v.rewardID,v.cnt)
    end
    msgRet.list[0] = len

    Msg.send(msgRet,human.fd)
end

-- 发放物品九零  一 起玩 www.9 01   7 5.com 
function getReward(human,day)
    checkDB(human)
    local signInDB = human.db.signIn
    local buyToday = checkBuy(human)
    local state = getState(day,signInDB,buyToday)
    if state == 0 or state == STATUS_TYPE4 or state == STATUS_TYPE2 then 
        return
    end

    local config = getConfig(human)

    local itemID = nil
    local itemCnt = nil

    itemID = config[day].rewardID
    itemCnt = config[day].cnt

    -- 改db
    signInDB.status[day] = signInDB.status[day] or 0
    signInDB.status[day] = signInDB.status[day] + 1
    BagLogic.cleanMomentItemList()
	BagLogic.updateMomentItem(1, itemID, itemCnt)
	BagLogic.addMomentItemList(human, "signInReward")
    query(human)
    
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3502)
        break
    end
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_LOGINSIGN)
end

function isRed(human)
    checkDB(human)
    local signInDB = human.db.signIn
    local buyToday = checkBuy(human)
    local config = getConfig(human)
    for k,v in ipairs(config) do
        local state = getState(k,signInDB,buyToday)
        if state == STATUS_TYPE1 or state == STATUS_TYPE3  then 
            return true
        end
    end
end

function chargeAfter(human)
    for k, v in pairs(funcID) do
        YunYingLogic.updateIcon(YYInfo[k], human)
        YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3502)
        break
    end
end
